const http = require('http');

function post(path, body){
  return new Promise((resolve,reject)=>{
    const data = JSON.stringify(body);
    const opts = {
      hostname: '127.0.0.1', port: 3000, path, method: 'POST',
      headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(data) }
    };
    const req = http.request(opts, res=>{ let s=''; res.on('data',c=>s+=c); res.on('end',()=>resolve({code:res.statusCode, body:s})); });
    req.on('error', reject);
    req.write(data); req.end();
  });
}

function get(path, token){
  return new Promise((resolve,reject)=>{
    const opts = { hostname:'127.0.0.1', port:3000, path, method:'GET', headers:{ Authorization: 'Bearer '+token } };
    const req = http.request(opts, res=>{ let s=''; res.on('data',c=>s+=c); res.on('end',()=>resolve({code:res.statusCode, body:s})); });
    req.on('error', reject); req.end();
  });
}

(async()=>{
  const login = await post('/api/v1/login', { username:'admin', password:'123456', usertype:'1' });
  console.log('LOGIN', login.code, login.body);
  const token = JSON.parse(login.body).data.token;
  const menus = await get('/api/v1/menus', token);
  console.log('MENUS', menus.code, menus.body.substring(0,200)+'...');
})();
